package com.cts.c2.services;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.web.client.RestTemplate;

import android.util.Log;

import com.cts.c2.vo.Mystream;
import com.cts.c2.vo.SendNotificationVO;

public class StreamService {
	private String domain = "https://mashupmobilest4.cognizant.com";
	private String service = "MobileNewService/NotificationsandSSDSPermissions.asmx";
	private String method = "MyCweetsCommunityByID_New";

	RestTemplate restTemplate;
	Mystream result = null;

	public StreamService() {
		restTemplate = new RestTemplate();
	}

	public Mystream getMyStream(String strCweetURL, String CommunityID,
			String filter, String pagesize, String PageNo, String strUserID,
			String BBDeviceID) {
		try {
			// String url
			// =domain+"/"+service+"/"+"?strCweetURL=Site29;175931&CommunityID=&filter=&pagesize=5&PageNo=1&strUserID=175931&BBDeviceID=12345";
			String url = "https://mashupmobilest4.cognizant.com/MobileNewService/NotificationsandSSDSPermissions.asmx/MyCweetsCommunityByID_New?strCweetURL=Site29;175931&CommunityID=&filter=&pagesize=5&PageNo=1&strUserID=175931&BBDeviceID=12345";
			result = restTemplate.getForObject(url, Mystream.class);
			Log.i("response from server", result.getNotifications().getList()
					.get(0).getFromId());
		} catch (Exception e) {
			Log.e(this.getClass().getName(),
					"Error occured in contacting C2 webservice " + e);
		}
		return result;
	}

	public String sendNotification(SendNotificationVO notificationVO) {
		String result = null;
		String line = "";
		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"https://mashupmobilest4.cognizant.com/MobileNewService/NotificationsandSSDSPermissions.asmx/SendNotification");

		try {

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
					25);
			nameValuePairs.add(new BasicNameValuePair("Title", notificationVO
					.getTitle()));
			nameValuePairs.add(new BasicNameValuePair("FromId", notificationVO
					.getFromId()));
			nameValuePairs.add(new BasicNameValuePair("ToId", notificationVO
					.getToId()));
			nameValuePairs.add(new BasicNameValuePair("Context", notificationVO
					.getContext()));
			nameValuePairs.add(new BasicNameValuePair("FromName",
					notificationVO.getFromName()));
			nameValuePairs.add(new BasicNameValuePair("ToName", notificationVO
					.getToName()));
			nameValuePairs.add(new BasicNameValuePair("status", notificationVO
					.getStatus()));
			nameValuePairs.add(new BasicNameValuePair("LastModifiedDate",
					notificationVO.getLastModifiedDate()));
			nameValuePairs.add(new BasicNameValuePair("EntityType",
					notificationVO.getEntityType()));
			nameValuePairs.add(new BasicNameValuePair("Comment", notificationVO
					.getComment()));
			nameValuePairs.add(new BasicNameValuePair("Hide", notificationVO
					.getHide()));
			nameValuePairs.add(new BasicNameValuePair("NotificationText",
					notificationVO.getNotificationText()));
			nameValuePairs.add(new BasicNameValuePair("NotificationTitle",
					notificationVO.getNotificationTitle()));
			nameValuePairs.add(new BasicNameValuePair("StartDate",
					notificationVO.getStartDate()));
			nameValuePairs.add(new BasicNameValuePair("EndDate", notificationVO
					.getEndDate()));
			nameValuePairs.add(new BasicNameValuePair("Featured",
					notificationVO.getFeatured()));
			nameValuePairs.add(new BasicNameValuePair("ItemLink",
					notificationVO.getItemLink()));
			nameValuePairs.add(new BasicNameValuePair("CreatedBy",
					notificationVO.getCreatedBy()));
			nameValuePairs.add(new BasicNameValuePair("ModifiedBy",
					notificationVO.getModifiedBy()));
			nameValuePairs.add(new BasicNameValuePair("CommunityName",
					notificationVO.getCommunityName()));
			nameValuePairs.add(new BasicNameValuePair("CommunityID",
					notificationVO.getCommunityID()));
			nameValuePairs.add(new BasicNameValuePair("Latest", notificationVO
					.getLatest()));
			nameValuePairs.add(new BasicNameValuePair("EntityId",
					notificationVO.getEntityId()));
			nameValuePairs.add(new BasicNameValuePair("SIPAddress",
					notificationVO.getSIPAddress()));
			nameValuePairs.add(new BasicNameValuePair("BBDeviceID",
					notificationVO.getBBDeviceID()));

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse response = httpclient.execute(httppost);
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));

			while ((line = rd.readLine()) != null) {
				System.out.println("Response from server " + line);
			}

		} catch (ClientProtocolException e) {
			System.out
					.println("Exception occured while posting Cweet to C2 web services "
							+ e);
		} catch (IOException e) {
			System.out
					.println("Exception occured while posting Cweet to C2 web services "
							+ e);
		}
		result = line;
		return result;
	}
}
